// pages/service/service.js

// 获取应用实例
const app = getApp()
const api = require('../../../utils/util')
Page({
    // 页面分享
    onShareAppMessage () {},
    // 分享朋友圈
    onShareTimeline () {},

    /**
     * 页面的初始数据
     */
    data: {
        imagesUrl: app.globalData.imagesUrl,
        // 搜素
        value: '',
        // 列表数据
        list1: [
          
        ],
        list2: [],
        list1Id: 1,
        // 切换置顶
        tabsList: [
            {
                title: '文化传媒'
            }
        ],
        tabsCurrent: 0,
        typeCurrent:0,
        // 分类弹窗
        typePopList: [
            {
                txt: '文化传媒'
            },
            {
                txt: '数字科技'
            },
            {
                txt: '法律咨询'
            },
            {
                txt: '房产建筑'
            },
            {
                txt: '物流贸易'
            },
            {
                txt: '专业咨询'
            },
            {
                txt: '医疗制药'
            },
            {
                txt: '机械制造'
            },
            {
                txt: '能源化工'
            },
            {
                txt: '物流贸易'
            },
            {
                txt: '消费零售'
            },
            {
                txt: '商务金融'
            },
            {
                txt: '土木工程'
            },
            {
                txt: '互联网通讯'
            }
        ],
        typePopShow: false,
        page: 1,
        category: ''
    },
    onLoad(options) {
        if(options.str){
          this.setData({
            value: options.str
          })
        }
        this.setData({
          category: options.category
        },()=>{
          
        })
        let title = '全部'
        if(this.data.category){
            title = options.category
        }
        this.setData({
          tabsList:[{
            title: title
          }],
        })
        let that = this
        
        this.category(that, options.category)
    },
    // 刷新新瀑布流
    update(isFlag) {
        // 重置瀑布流组件
        if(isFlag){
          //this.selectComponent('#waterfall').reset()
        }
      
        let list1 = this.getMockData()
        this.setData({ list1 },()=> {
          console.log(this.data.list1)
          wx.hideLoading();
            wx.stopPullDownRefresh()
        })
        //this.selectComponent('#waterfall').reset()
       
    },
    onShow(){
      let that = this
      this.setData({
        page:1
      })
      wx.pageScrollTo({scrollTop: 0, duration: 200})
      this.product(that, 1)
    },
    //获取产品
    product(that, page){
      let param = {
        str: that.data.value,
        category: that.data.category
      }
      api.product_listp(param, page).then(function(res){
        
        if (res.data.length>0){
          let list2 = []
          for(let i = 0; i< res.data.length; i++){
            list2.push({
              url: res.data[i].url,
              title: res.data[i].title+'',
              price: res.data[i].price+'',
              companyLogoUrl: res.data[i].logo,
              companyTitle: res.data[i].companyName+'',
              tag: res.data[i].category+'',
              is_discuss: res.data[i].is_discuss+'',
              id: res.data[i].id,
              sub_title:res.data[i].sub_title
            })
          }
          that.setData({
            list2: page == 1 ? list2 : that.data.list2.concat(list2),//合并数据
            page: page + 1
         },()=>{
           if(page > 1 ){
            that.update(false)
           }else{
             that.update(true)
           }
          
         })
          
        }else{
          wx.hideLoading();
            wx.showToast({
              title: '没有更多数据了',
              ico: 'error'
            })
        }
      
      })
    },
    
    //获取分类
    category(that, category){
    
      api.category_list().then(function(res){
        let typePopList = [];
      
        for(let i = 0; i< res.data.length; i++){
          typePopList.push({
            txt: res.data[i].title
          })
        }
         that.setData({
          typePopList
         },()=>{
          for(let i = 0 ; i < that.data.typePopList.length; i++ ){
            if(that.data.typePopList[i].txt == category){
               that.setData({
                typeCurrent: i 
               })
               console.log(i)
            }
          }
         })
      })
    },
    // 获取瀑布流数据
    getMockData() {
        let { list1Id } = this.data
        let list1 = []
        for (let i = 0; i < this.data.list2.length; i++) {
            list1.push({
                list1Id,
                imgUrl: app.globalData.siteUrl + this.data.list2[i].url,
                title: this.data.list2[i].title+'',
                price: this.data.list2[i].price+'',
                companyLogoUrl: app.globalData.siteUrl + this.data.list2[i].companyLogoUrl,
                companyTitle: this.data.list2[i].companyTitle+'',
                tage: this.data.list2[i].tag+'',
                is_discuss: this.data.list2[i].is_discuss+'',
                id: this.data.list2[i].id,
                sub_title: this.data.list2[i].sub_title
            })

            this.data.list1Id = ++list1Id
        }
        return list1
    },
    // 切换置顶
    onStickyScroll(e) {
        let stickyScroll = false
        if(e.detail.isFixed) {
            stickyScroll = !stickyScroll
        }
        this.setData({stickyScroll})
    },
    // 切换
    onTabsClick(e) {
        let tabsCurrent = e.target.dataset.index
        this.setData({tabsCurrent})
    },
    // 分类弹窗
    typeShowPopup() {
        this.setData({ typePopShow: true });
    },
    onChange(e){
      this.data.value = e.detail.value
    },
    onSearch(){
      console.log(this.data.value)
    },
    onPullDownRefresh:function(){
      wx.showLoading();
      this.onRefresh();
    
    },
    onRefresh:function(){
        let that = this
        //导航条加载动画
        //wx.showNavigationBarLoading();
        wx.showLoading();
        setTimeout(function () {
          wx.hideNavigationBarLoading();
          //停止下拉刷新
          that.setData({
            page: 1,
            list1Id: 1
      
          })
          that.product(that, 1)
          
        }, 2000);
      },

      onReachBottom:function(){
       wx.showLoading()
        // 加载更多，滑动到底部执行此方法
        let page = this.data.page ;
        console.log(page)
        let that = this
        this.product(that, page)
    }
})